package simple;

/*
 * @(#)SimpleRead.java	1.12 98/06/25 SMI
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Sun grants you ("Licensee") a non-exclusive, royalty free, license 
 * to use, modify and redistribute this software in source and binary
 * code form, provided that i) this copyright notice and license appear
 * on all copies of the software; and ii) Licensee does not utilize the
 * software in a manner which is disparaging to Sun.
 *
 * This software is provided "AS IS," without a warranty of any kind.
 * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES,
 * INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND
 * ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
 * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE
 * SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS
 * BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT,
 * INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES,
 * HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING
 * OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * This software is not designed or intended for use in on-line control
 * of aircraft, air traffic, aircraft navigation or aircraft
 * communications; or in the design, construction, operation or
 * maintenance of any nuclear facility. Licensee represents and
 * warrants that it will not use or redistribute the Software for such
 * purposes.
 */

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;


import Utils.Utils;

public class SimpleRead extends JFrame implements Runnable, SerialPortEventListener {
	private static final long serialVersionUID = 1L;

	SerialPort port;

	InputStream inputStream;
	Thread readThread;

	StringBuilder data = new StringBuilder();

	boolean dataAvailable = false;

	JButton connectButton;
	JButton disconnectButton;
	JList commList;

	JButton sendMessage;

	public static void main(String[] args) {
		SimpleRead s = new SimpleRead();
		s.setSize(400, 400);
		s.setVisible(true);
	}

	public SimpleRead() {
		super("JSpoorloos");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLayout(new BorderLayout());

		DefaultListModel model = new DefaultListModel();
		
		// Get the CommPortIdentifiers:
		Enumeration<?> e = CommPortIdentifier.getPortIdentifiers();
		while (e.hasMoreElements()) {
			CommPortIdentifier commPort = (CommPortIdentifier) e.nextElement();
			// Only include the serial ports that are not currently owned by any software:
			if (commPort.getPortType() == CommPortIdentifier.PORT_SERIAL && !commPort.isCurrentlyOwned()) {
				model.addElement(commPort);
			}
		}

		commList = new JList(model);
		this.add(commList, BorderLayout.CENTER);
		commList.setCellRenderer(new ListCellRenderer() {

			@Override
			public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
				try {
					JLabel label = new JLabel();
					label.setOpaque(true);
					label.setText(((CommPortIdentifier) value).getName());
					if (isSelected) {
						label.setForeground(list.getSelectionForeground());
						label.setBackground(list.getSelectionBackground());
					} else {
						label.setForeground(list.getForeground());
						label.setBackground(list.getBackground());
					}
					return label;
				} catch (ClassCastException e) {
					System.out.println(e.getMessage());
				}
				return null;
			}

		});
		commList.addListSelectionListener(new ListSelectionListener() {

			@Override
			public void valueChanged(ListSelectionEvent e) {
				if (!e.getValueIsAdjusting()) {
					System.out.println(e.getSource());
				}
			}

		});

		JPanel buttonPanel = new JPanel(new BorderLayout());
		this.add(buttonPanel, BorderLayout.SOUTH);

		connectButton = new JButton("Connect");
		buttonPanel.add(connectButton, BorderLayout.NORTH);
		connectButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent ae) {
				if (commList.getSelectedValue() != null) {
					try {
						CommPortIdentifier commPortId = (CommPortIdentifier) commList.getSelectedValue();

						port = (SerialPort) commPortId.open("JSpoorloos", 5000);
						commList.setBackground(Color.ORANGE);

						port.addEventListener(SimpleRead.this);

						inputStream = port.getInputStream();

						port.notifyOnDataAvailable(true);

						port.setSerialPortParams(115200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

						readThread = new Thread(SimpleRead.this);
						readThread.start();

					} catch (PortInUseException e1) {
						e1.printStackTrace();
					} catch (TooManyListenersException e) {
						e.printStackTrace();
					} catch (UnsupportedCommOperationException e) {
						e.printStackTrace();
					} catch (ClassCastException exp) {
						System.out.println(exp.getMessage());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		});

		disconnectButton = new JButton("Disconnect");
		buttonPanel.add(disconnectButton, BorderLayout.CENTER);
		disconnectButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				if (port != null) {
					port.close();
					commList.setBackground(Color.WHITE);
				}
			}
		});

		sendMessage = new JButton("Send message");
		buttonPanel.add(sendMessage, BorderLayout.SOUTH);
		sendMessage.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				try {
					OutputStream out = port.getOutputStream();
//					 String NMEA = "$" + "PMTK182,7," + Integer.toHexString(0) + "," + Integer.toHexString(65536) + "*";
					String NMEA = "$" + "PMTK182,7," + Integer.toHexString(0) + "," + Integer.toHexString(20) + "*";
//					String NMEA = "$" + "PMTK182,1,3,1" + Integer.toHexString(0) + "," + Integer.toHexString(20) + "*";
//					String NMEA = "$" + "PMTK182,6,1*";
					NMEA += Utils.Checksum(NMEA) + "\r\n";
					// '\015', '\012'
					out.write(NMEA.getBytes());

				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
	}

	public void run() {
		StringBuilder data = new StringBuilder();
		boolean doOnce = true;
		while (true) {
			// Remember that CR+LF is a Newline. see
			// http://en.wikipedia.org/wiki/Newline
			// CR = 13
			// LF = 10
			try {
				byte[] bytes = new byte[0];
				if (inputStream.available() > 0) {
					bytes = new byte[inputStream.available()];
					int numberRead = inputStream.read(bytes);
					
					data.append(new String(bytes));

					// Uncomment the sentence below if you want "real time" GPS
					// feedback:
					// System.out.print(new String(bytes));
				}

				// Interpret the data:
				Pattern startPattern = Pattern.compile("^(\\$PMTK182)", Pattern.MULTILINE);
				Pattern endPattern = Pattern.compile("(\\*\\w{2})$", Pattern.MULTILINE);
				Matcher startMatcher = startPattern.matcher(data);

				Pattern dynamicSettingPattern = Pattern.compile("(AA){7}(\\w){10}(BB){4}", Pattern.MULTILINE);

				while (startMatcher.find()) {
					Matcher endMatcher = endPattern.matcher(data);
					if (endMatcher.find(startMatcher.end())) {
						String sentence = data.subSequence(startMatcher.start(), endMatcher.end()).toString();
						System.out.println(sentence);
						data.delete(0, endMatcher.end());
						startMatcher = startPattern.matcher(data);

						String subSentence = sentence.subSequence(sentence.lastIndexOf(",") + 1, sentence.length() - 3).toString();
						// System.out.println(subSentence);

						if (doOnce) {
							doOnce = false;
							
							// Get the double character Strings:
							String[] myBytes = new String[subSentence.length() / 2];
							for (int i = 0; i < subSentence.length(); i += 2) {
								myBytes[i / 2] = subSentence.substring(i, i + 2);
							}
							
							// Convert the double character Strings to bytes:
							byte[] myRealBytes = new byte[myBytes.length];
							for (int i = 0; i < myBytes.length; i++) {
								myRealBytes[i] = (byte)Short.parseShort(myBytes[i], 16);
							}
							DataInputStream myInputStream = new DataInputStream(new ByteArrayInputStream(myRealBytes));

							// LOG COUNT:
							// short logCount = Short.decode("0x"+myBytes[1]+myBytes[0]);
//							int logCount = Integer.parseInt(myBytes[1], 16) << 8 | Integer.parseInt(myBytes[0], 16);
							int logCount = Short.reverseBytes(myInputStream.readShort());
							System.out.println(logCount == -1 ? "logCount = NOT FULL" : "logCount = " + logCount);

							// FORMAT:
							// int format = Integer.decode("0x"+myBytes[5]+myBytes[4]+myBytes[3]+myBytes[2]);
//							int format = Integer.parseInt(myBytes[5], 16) << 24 | Integer.parseInt(myBytes[4], 16) << 16 | Integer.parseInt(myBytes[3], 16) << 8 | Integer.parseInt(myBytes[2], 16);
							int format = Integer.reverseBytes(myInputStream.readInt());
							System.out.println("format = " + format);
							boolean[] bits = new boolean[32]; // 32 bits in an
																// int
							for (int i = 0; i < 32; i++) {
								bits[i] = (format & (1 << i)) != 0;
							}
							for (int i = 0; i < bits.length; i++) {
								System.out.print(i + ":");
								System.out.print(bits[i] ? 1 : 0);
								System.out.print(" ");
							}
							System.out.println();

							// MODE:
							// short mode = Short.decode("0x"+myBytes[7]+myBytes[6]);
//							int mode = Integer.parseInt(myBytes[7], 16) << 8 | Integer.parseInt(myBytes[6], 16);
							short mode = Short.reverseBytes(myInputStream.readShort());
							System.out.println("mode = " + mode);

							// LOG PERIOD in 1/10th sec:
							// int period = Integer.decode("0x"+myBytes[11]+myBytes[10]+myBytes[9]+myBytes[8]);
//							int period = Integer.parseInt(myBytes[9], 16) << 8 | Integer.parseInt(myBytes[8], 16);
							int period = Integer.reverseBytes(myInputStream.readInt());
							System.out.println("period = " + period);

							// LOG DISTANCE in 1/10th meter:
							// int distance = Integer.decode("0x"+myBytes[15]+myBytes[14]+myBytes[13]+myBytes[12]);
//							int distance = Integer.parseInt(myBytes[13], 16) << 8 | Integer.parseInt(myBytes[12], 16);
							int distance = Integer.reverseBytes(myInputStream.readInt());
							System.out.println("distance = " + distance);

							// LOG SPEED in 1/10th km/h:
							// int speed = Integer.decode("0x"+myBytes[19]+myBytes[18]+myBytes[17]+myBytes[16]);
//							int speed = Integer.parseInt(myBytes[17], 16) << 8 | Integer.parseInt(myBytes[16], 16);
							int speed = Integer.reverseBytes(myInputStream.readInt());
							System.out.println("speed = " + speed);

							// Find the start of the LogChunks. Look for the end of the Dynamic Setting Pattern:
							int startOfLogChunks = 0;
							System.out.println("Start of LogChunks = " + startOfLogChunks);
							Matcher dynamicSettingMatcher = dynamicSettingPattern.matcher(subSentence);
							if (dynamicSettingMatcher.find()) {
								startOfLogChunks = dynamicSettingMatcher.end() / 2;
							}
							
							// I have to skip the "running count" inputStream over the area I don't want to read (for now):
							myInputStream.skipBytes(32); // skip the FSEC
							myInputStream.skipBytes(454); // skip the reserves area
							myInputStream.skipBytes(6); // skip the header's tail: 2A0FBBBBBBBB
							myInputStream.skipBytes(16); // skip the dynamic setting pattern: AAAAAAAAAAAAAA0706010000BBBBBBBB

							int offset = startOfLogChunks;
							if (bits[0] == true) {
//								int date = Integer.parseInt(myBytes[offset + 3], 16) << 24 | Integer.parseInt(myBytes[offset + 2], 16) << 16 | Integer.parseInt(myBytes[offset + 1], 16) << 8 | Integer.parseInt(myBytes[offset], 16);
								int date = Integer.reverseBytes(myInputStream.readInt());
								long dateMilli = ((long) date) * 1000;
								/*
								 * cast to long, because multiplying a int with
								 * 1000 pushed it over the int's max value.
								 * (Multiply with 1000 to convert from seconds
								 * to milliseconds)
								 */
								System.out.println("Date = " + new Date(dateMilli));
								offset += 4;
							}

							if (bits[1] == true) {
//								short valid = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short valid = Short.reverseBytes(myInputStream.readShort());
								boolean noFixMode = (valid & (1 << 0)) > 0;
								System.out.print("noFixMode = "+noFixMode);
								boolean spsMode = (valid & (1 << 1)) > 0;
								System.out.print(" ### spsMode = "+spsMode); // Standard Positioning Service
								boolean dgpsMode = (valid & (1 << 2)) > 0;
								System.out.print(" ### dgpsMode = "+dgpsMode); // Differential Global Positioning System (DGPS) is an enhancement to Global Positioning System
								boolean ppsMode = (valid & (1 << 3)) > 0;
								System.out.print(" ### ppsMode = "+ppsMode); // Precise Positioning Service
								boolean rtkMode = (valid & (1 << 4)) > 0;
								System.out.print(" ### rtkMode = "+rtkMode); // Real Time Kinematic (RTK) satellite navigation
								boolean frtkMode = (valid & (1 << 5)) > 0;
								System.out.print(" ### frtkMode = "+frtkMode);
								boolean estimatedMode = (valid & (1 << 6)) > 0;
								System.out.print(" ### estimatedMode = "+estimatedMode);
								boolean manualInputMode = (valid & (1 << 7)) > 0;
								System.out.print(" ### manualInputMode = "+manualInputMode);
								boolean simulatorMode = (valid & (1 << 8)) > 0;
								System.out.println(" ### simulatorMode = "+simulatorMode);
								offset += 2;
							}

							// Latitude:
							if (bits[2] == true) {
//								long latitudeLong = Long.parseLong(myBytes[offset + 7] + myBytes[offset + 6] + myBytes[offset + 5] + myBytes[offset + 4] + myBytes[offset + 3] + myBytes[offset + 2] + myBytes[offset + 1] + myBytes[offset], 16);
								long latitudeLong = Long.reverseBytes(myInputStream.readLong());
								double latitudeDegrees = Double.longBitsToDouble(latitudeLong);
								System.out.println("Latitude = " + latitudeDegrees + " " + (latitudeDegrees > 0 ? "N" : "S"));
								offset += 8;
							}
							
							// Logitude:
							if (bits[3] == true) {
//								long longitudeLong = Long.parseLong(myBytes[offset + 7] + myBytes[offset + 6] + myBytes[offset + 5] + myBytes[offset + 4] + myBytes[offset + 3] + myBytes[offset + 2] + myBytes[offset + 1] + myBytes[offset], 16);
								long longitudeLong = Long.reverseBytes(myInputStream.readLong());
								double longitudeDegrees = Double.longBitsToDouble(longitudeLong);
								System.out.println("Longitude = " + longitudeDegrees + " " + (longitudeDegrees > 0 ? "E" : "W"));
								offset += 8;
							}

							if (bits[4] == true) {
//								int heightInt = Integer.parseInt(myBytes[offset + 3] + myBytes[offset + 2] + myBytes[offset + 1] + myBytes[offset], 16);
								int heightInt = Integer.reverseBytes(myInputStream.readInt());
								float heightMeters = Float.intBitsToFloat(heightInt);
								System.out.println("Height = " + heightMeters + " meters");
								offset += 4;
							}

							if (bits[5] == true) {
//								int speedInt = Integer.parseInt(myBytes[offset + 3] + myBytes[offset + 2] + myBytes[offset + 1] + myBytes[offset], 16);
								int speedInt = Integer.reverseBytes(myInputStream.readInt());
								float speedKmPerHour = Float.intBitsToFloat(speedInt);
								System.out.println("Speed in km/h = " + speedKmPerHour);
								offset += 4;
							}

							if (bits[6] == true) {
//								int trackInt = Integer.parseInt(myBytes[offset + 3] + myBytes[offset + 2] + myBytes[offset + 1] + myBytes[offset], 16);
								int trackInt = Integer.reverseBytes(myInputStream.readInt());
								float trackDegrees = Float.intBitsToFloat(trackInt);
								System.out.println("Track = " + trackDegrees);
								offset += 4;
							}
							if (bits[7] == true) {
								// See:
								// http://www.leicaus.com/support/ReferenceStations1200/QuickGuides/Intro%20to%20GPS%20Reference%20Stations.pdf
//								short dsta = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short dsta = Short.reverseBytes(myInputStream.readShort());
								System.out.println("Diffential GPS reference station ID = "+dsta);
								offset += 2;
							}
							if (bits[8] == true) {
								// See:
								// http://www.leicaus.com/support/ReferenceStations1200/QuickGuides/Intro%20to%20GPS%20Reference%20Stations.pdf 
//								int dage = Integer.parseInt(myBytes[offset+3]+myBytes[offset+2]+myBytes[offset+1]+myBytes[offset], 16);
								int dage = Integer.reverseBytes(myInputStream.readInt());
								float dageFloat = Float.intBitsToFloat(dage);
								System.out.println("Differential GPS correction data age = "+dageFloat);
								offset += 4;
							}
							if (bits[9] == true) {
//								short pdop = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short pdop = Short.reverseBytes(myInputStream.readShort());
								System.out.println("Positional Dillution of Precision = "+pdop);
								offset += 2;
							}
							if (bits[10] == true) {
//								short hdop = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short hdop = Short.reverseBytes(myInputStream.readShort());
								System.out.println("Horizontal Dillution of Precision = "+hdop);
								offset += 2;
							}
							if (bits[11] == true) {
//								short vdop = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short vdop = Short.reverseBytes(myInputStream.readShort());
								System.out.println("Vertical Dillution of Precision = "+vdop);
								offset += 2;
							}

							byte numberOfSatellitesInView = -1;
							if (bits[12] == true) {
//								numberOfSatellitesInView = Byte.parseByte(myBytes[offset], 16);
								numberOfSatellitesInView = myInputStream.readByte();
								System.out.println("Number of satellites in view = " + numberOfSatellitesInView);
//								int numberOfSatellitesInUse = Byte.parseByte(myBytes[offset + 1], 16);
								byte numberOfSatellitesInUse = myInputStream.readByte();
								System.out.println("Number of satellites in use = " + numberOfSatellitesInUse);
								offset += 2;
							}
							if (numberOfSatellitesInView != -1) {
								for (int i = 0; i < numberOfSatellitesInView; i++) {
									if (bits[13] == true) {
//										int satelliteID = Byte.parseByte(myBytes[offset], 16);
										byte satelliteID = myInputStream.readByte();
										System.out.print("Satellite ID in view = " + satelliteID);
										
//										boolean satelliteInUse = (Byte.parseByte(myBytes[offset+1], 16) & (1 << 0)) > 0;
										boolean satelliteInUse = (myInputStream.readByte() & (1 << 0)) > 0;
										System.out.print(" -> Satellite in use = " + satelliteInUse);
										
//										int numberOfSatellitesInViewAgain = Byte.parseByte(myBytes[offset+2], 16);
										byte numberOfSatellitesInViewAgain = myInputStream.readByte();
										System.out.print(" -> Check = " + numberOfSatellitesInViewAgain);
										offset += 4;
										
										myInputStream.skipBytes(1); // this byte is empty (die een was 'n vuil bek!)
										
										// Elevation angle:
//										int elevationAngle = Integer.parseInt(myBytes[offset + 1]+myBytes[offset],16);
										short elevationAngle = Short.reverseBytes(myInputStream.readShort());
										System.out.print(" -> Elevation Angle = " + elevationAngle);
										offset += 2;

										// Azimuth:
//										int azimuth = Integer.parseInt(myBytes[offset + 1]+myBytes[offset],16);
										short azimuth = Short.reverseBytes(myInputStream.readShort());
										System.out.print(" -> Azimuth = " + azimuth);
										offset += 2;

										// SNR:
//										int signalNoiseRatio = Integer.parseInt(myBytes[offset + 1]+myBytes[offset],16);
										short signalNoiseRatio = Short.reverseBytes(myInputStream.readShort());
										System.out.println(" -> Signal to Noise Ratio = " + signalNoiseRatio);
										offset += 2;
									}
								}
							}
							
// 						if (bits[14] == true) {
//							offset += 2;
//						}
//						if (bits[15] == true) {
//							offset += 2;
//						}
//						if (bits[16] == true) {
//							offset += 2;
//						}
							
							if (bits[17] == true) {
//								short rcr = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short rcr = Short.reverseBytes(myInputStream.readShort());
								boolean recordedByTime = (rcr & (1 << 0)) > 0;
								System.out.print("Recorded by Time = "+recordedByTime);
								boolean recordedBySpeed = (rcr & (1 << 1)) > 0;
								System.out.print(" ### Recorded by Speed = "+recordedBySpeed);
								boolean recordedByDistance = (rcr & (1 << 2)) > 0;
								System.out.print(" ### Recorded by Distance = "+recordedByDistance);
								boolean recordedByButton = (rcr & (1 << 3)) > 0;
								System.out.println(" ### Recorded by Button = "+recordedByButton);
								offset += 2;
							}
							if (bits[18] == true) {
//								short milliSeconds = Short.parseShort(myBytes[offset+1]+myBytes[offset], 16);
								short milliSeconds = Short.reverseBytes(myInputStream.readShort());
								System.out.println("Milli seconds = "+milliSeconds);
								offset += 2;
							}
							if (bits[19] == true) {
								long distanceLong = Long.reverseBytes(myInputStream.readLong());
								double distanceTraveledSinceLastLogPointInMeters = Double.longBitsToDouble(distanceLong);
								System.out.println("Distance travelled = "+distanceTraveledSinceLastLogPointInMeters+" meters");
								offset += 8;
							}

							System.out.println(myBytes[offset]);
						}
					} else {

					}
				}


			} catch (IOException e) {
			}

			try {
				Thread.sleep(100);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public void serialEvent(SerialPortEvent event) {

		switch (event.getEventType()) {
		case SerialPortEvent.BI:
		case SerialPortEvent.OE:
		case SerialPortEvent.FE:
		case SerialPortEvent.PE:
		case SerialPortEvent.CD:
		case SerialPortEvent.CTS:
		case SerialPortEvent.DSR:
		case SerialPortEvent.RI:
		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;
		case SerialPortEvent.DATA_AVAILABLE:
			dataAvailable = true;
			break;
		}
	}

}
